package cc.aliza.lib.mongo.core.encoder;

import cc.aliza.lib.mongo.core.annotations.Embed;
import cc.aliza.lib.mongo.core.utils.MapperUtil;

import java.lang.reflect.Field;

/**
 * 『给时光以生命·给岁月以文明』
 *
 * @author Zhang Jing (byrdkm17@gmail.com).
 */
public class EmbedEncoder extends AbstractEncoder {

    Embed embed;

    public EmbedEncoder(Object obj, Field field) {
        super(obj, field);
    }

    @Override
    public String getFieldName() {
        embed = field.getAnnotation(Embed.class);
        return getFieldRealName(embed.name());
    }

    @Override
    public Object encode() {
        return MapperUtil.toDocument(value, true);
    }
}
